﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Acme.BookStore.Sku;
using Acme.BookStore.Skuss;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore
{
    public class SkuAppService: ApplicationService, ISkuService
    {
        private readonly IRepository<tbCommoditySpecifica, int> _tbCommoditySpecificas;
        public SkuAppService(IRepository<tbCommoditySpecifica, int> tbCommoditySpecificas)
        {
            _tbCommoditySpecificas = tbCommoditySpecificas;
        }

        public int CreateSku(CreateUpdateSkuDto dto)
        {
            //string op = "";
            //foreach (var item in dto.CommodSmallNamex)
            //{
            //    op += item.ToString() + ',';
            //}
            //op = op.Substring(0, (op.Length) - 1);
            var user = new tbCommoditySpecifica()
            {
                CommodSpeciName = dto.CommodSpeciName,
                CommodSmallName = dto.CommodSmallName,
                CommoditySalePrice = dto.CommoditySalePrice,
                StockNum = dto.StockNum
            };
            _tbCommoditySpecificas.InsertAsync(user);
            return 1;
        }

        public PagedResultDto<SkuListDto> GetSku(SkuQueryDto input)
        {
            var get = from a in _tbCommoditySpecificas
                      select new SkuListDto
                      {
                          Id = a.Id,
                          CommodSmallName = a.CommodSmallName,
                          CommodSpeciName = a.CommodSpeciName,
                          CommoditySalePrice = a.CommoditySalePrice,
                          StockNum = a.StockNum
                      };
            var list = get.WhereIf(input.Id > 0, s => s.Id.Equals(input.Id)).ToList();
            var totacount = list.Count();
            return new PagedResultDto<SkuListDto>(totacount, list);
        }

    }
}
